package top.suven.base.http.admin.frame.config;

import top.suven.base.http.enums.SysMsgEnumType;
import top.suven.base.util.utilize.JsonUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 结果返回
 */
public class ResponseResult implements Serializable {

    private int code = 0;     //状态码
    private String msg = "";     //状态描述
    private long times = System.currentTimeMillis();        //时间戳 13位
    private Object data = new HashMap<>();    //Json 内容

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public long getTimes() {
        return times;
    }

    public void setTimes(long times) {
        this.times = times;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public ResponseResult put(String key, Object value){
        if(this.getData() instanceof Map){
            ((Map) this.getData()).put(key, value);
        }else {
            Map<String, Object> map = new HashMap<>();
            map.put(key, value);
            this.setData(map);
        }
        return this;
    }

    public ResponseResult putAll(Map<String, Object> data){
        if(data != null && !data.isEmpty()){
            for (Map.Entry<String, Object> entry : data.entrySet()) {
                this.put(entry.getKey(), entry.getValue());
            }
        }
        return this;
    }

    public static ResponseResult ok(String msg, Object data){
        ResponseResult result = new ResponseResult();
        if(msg != null)
            result.setMsg(msg);
        if(data != null)
            result.setData(data);
        return result;
    }

    public static ResponseResult ok(Object data){
        return ResponseResult.ok(null, data);
    }

    public static ResponseResult ok(){
        return ResponseResult.ok(null);
    }

    public static ResponseResult error(int code, String msg, Object data){
        ResponseResult result = new ResponseResult();
        result.setCode(code);
        if(msg != null)
            result.setMsg(msg);
        if(data != null)
            result.setData(data);
        return result;
    }

    public static ResponseResult error(int code, String msg){
        return ResponseResult.error(code, msg, null);
    }

    public static ResponseResult error(SysMsgEnumType sysMsgEnumType){
        return ResponseResult.error(sysMsgEnumType.getCode(), sysMsgEnumType.getMsg());
    }


    @Override
    public String toString() {
        return JsonUtils.toJsonString(this);
    }
}
